]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
extensions: Replace exported gettext functions
authorFlorian Müllner <fmuellner@gnome.org>
Sun, 16 Jul 2023 23:13:06 +0000 (01:13 +0200)
committerFlorian Müllner <fmuellner@gnome.org>
Sun, 30 Jul 2023 07:29:44 +0000 (10:29 +0300)
Use the new defineTranslationFunctions() method from the previous
commit to create gettext functions for the module, instead of
re-exporting from the shared module.

It is now up to extension developers to use the more effective

```js
import {Extension} from 'etensions/extension.js';
const {gettext: _} =
    Extension.defineTranslationFunctions(import.meta.url);
```

or the more convenient

```js
import {Extension, gettext} from 'extensions/extension.js';
const _ = gettext;
```

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>

js/extensions/extension.js
js/extensions/prefs.js
js/extensions/sharedInternals.js

index 966a8336bd5a5a04a4183e1d954bc4360a238b88..bcc518edd78f4e0be5bd76fbcd6faa1f62066985 100644 (file)
@@ -1,7 +1,5 @@
 import {ExtensionBase, GettextWrapper, setExtensionManager} from './sharedInternals.js';
 
-export {gettext, ngettext, pgettext} from './sharedInternals.js';
-
 const {extensionManager} = imports.ui.main;
 setExtensionManager(extensionManager);
 
@@ -24,3 +22,7 @@ export class Extension extends ExtensionBase {
         extensionManager.openExtensionPrefs(this.uuid, '', {});
     }
 }
+
+export const {
+    gettext, ngettext, pgettext,
+} = Extension.defineTranslationFunctions();
index db285e247c3abfcfd780e6ba8138051a5014f5cd..5d2de3f987fe3cdb5f91bd917b69ecdc659a8a0a 100644 (file)
@@ -5,8 +5,6 @@ import {extensionManager} from '../extensionsService.js';
 
 setExtensionManager(extensionManager);
 
-export {gettext, ngettext, pgettext} from './sharedInternals.js';
-
 export class ExtensionPreferences extends ExtensionBase {
     static defineTranslationFunctions(url) {
         const wrapper = new GettextWrapper(this, url);
@@ -22,3 +20,7 @@ export class ExtensionPreferences extends ExtensionBase {
         throw new GObject.NotImplementedError();
     }
 }
+
+export const {
+    gettext, ngettext, pgettext,
+} = ExtensionPreferences.defineTranslationFunctions();
index 2d5afd0f6644497c5dda2339ec8b16f35184fab0..dcac3c23310188a823f316e083583abc7fac2f31 100644 (file)
@@ -276,95 +276,3 @@ export class GettextWrapper {
         };
     }
 }
-
-/**
- * @private
- *
- * @returns {?object} - The current extension, or null if not called from
- * an extension.
- */
-function getCurrentExtension() {
-    const basePath = '/gnome-shell/extensions/';
-
-    // Search for an occurrence of an extension stack frame
-    // Start at 1 because 0 is the stack frame of this function
-    const [, ...stack] = new Error().stack.split('\n');
-    const extensionLine = stack.find(
-        line => line.includes(basePath));
-
-    if (!extensionLine)
-        return null;
-
-    // The exact stack line differs depending on where the function
-    // was called (function or module scope), and whether it's called
-    // from a module or legacy import (file:// URI vs. plain path).
-    //
-    // We don't have to care about the exact composition, all we need
-    // is a string that can be traversed as path and contains the UUID
-    let path = extensionLine.slice(extensionLine.indexOf(basePath));
-
-    // Walk up the directory tree, looking for an extension with
-    // the same UUID as a directory name.
-    do {
-        path = GLib.path_get_dirname(path);
-
-        const dirName = GLib.path_get_basename(path);
-        const extension = _extensionManager.lookup(dirName);
-        if (extension !== undefined)
-            return extension.stateObj;
-    } while (path !== '/');
-
-    return null;
-}
-
-/**
- * Translate @str using the extension's gettext domain
- *
- * @param {string} str - the string to translate
- *
- * @returns {string} - the translated string
- */
-export function gettext(str) {
-    return callExtensionGettextFunc('gettext', str);
-}
-
-/**
- * Translate @str and choose plural form using the extension's
- * gettext domain
- *
- * @param {string} str - the string to translate
- * @param {string} strPlural - the plural form of the string
- * @param {number} n - the quantity for which translation is needed
- *
- * @returns {string} - the translated string
- */
-export function ngettext(str, strPlural, n) {
-    return callExtensionGettextFunc('ngettext', str, strPlural, n);
-}
-
-/**
- * Translate @str in the context of @context using the extension's
- * gettext domain
- *
- * @param {string} context - context to disambiguate @str
- * @param {string} str - the string to translate
- *
- * @returns {string} - the translated string
- */
-export function pgettext(context, str) {
-    return callExtensionGettextFunc('pgettext', context, str);
-}
-
-/**
- * @private
- * @param {string} func - function name
- * @param {*[]} args - function arguments
- */
-function callExtensionGettextFunc(func, ...args) {
-    const extension = getCurrentExtension();
-
-    if (!extension)
-        throw new Error(`${func}() can only be called from extensions`);
-
-    return extension[func](...args);
-}